<template>
    <div class="city__main" ref="wrapper">
      <div>
        <div class="city__line">
          <div class="line__title">当前城市</div>
          <div class="city__content">
            <div class="itemWrap"  @click="handleCityChange()">
              <div class="cityItem">{{this.$store.state.city}}</div>
            </div>
          </div>
        </div>
        <div class="city__line">
          <div class="line__title">热门城市</div>
          <div class="city__content">
            <div class="itemWrap"
                 v-for="item of cityData.hotCities"
                 :key="item.id"
                 @click="handleCityChange(item.name)"
            >
              <div class="cityItem">{{item.name}}</div>
            </div>
          </div>
        </div>
        <div class="city__line">
          <div v-for="(city, key) of cityData.cities"
               :key="key"
               :ref="key"
          >
            <div class="line__title">{{key}}</div>
            <div>
              <div class="cityLine"
                   v-for="item of city"
                   :key="item.id"
                   @click="handleCityChange(item.name)"
              >{{item.name}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
  name: 'CityContent',
  props: {
    cityData: Object,
    letter: String
  },
  mounted () {
    this.$nextTick(() => {
      this.scroll = new Bscroll(this.$refs.wrapper, {
        mouseWheel: true, click: true, tap: true
      })
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  methods: {
    handleCityChange (msg) {
      this.$store.commit('change', msg)
      this.$router.push('/')
    }
  }
}
</script>

<style lang='scss' scoped>
  @import '~@/assets/style/common';
  .city__main {
    position: absolute;
    overflow: hidden;
    top: 184px;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
  }
  .line__title {
    padding: 16px 20px;
    background: #eee;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    color: #666;
    font-size: 24px;
  }
  .city__content {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    width: 100%;
    padding: 20px 30px 20px 20px;
  }
  .itemWrap {
    width: 30%
  }
  .cityItem {
    width: 100%;
    margin-right: 10px;
    margin-bottom: 16px;
    padding: 14px;
    border-radius: 6px;
    text-align: center;
    border: 1px solid #ccc;
    @include omit;
  }
  .cityLine {
    padding: 18px 30px 18px 20px;
    border-bottom: 1px solid #ccc;
  }
</style>
